package com.zhiyou.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.crud.bean.Donate;
import com.zhiyou.crud.bean.Real;
import com.zhiyou.crud.bean.User;
import com.zhiyou.crud.service.Admin_UserManageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: Admin_UserManageController
 * @Author: ZengS
 * @Date: 2022/9/20 17:16
 */
@Controller
public class Admin_UserManageController {

    @Resource
    Admin_UserManageService admin_userManageService;

    /**
     * 查询注册用户
     * 模糊检索条件，
     * 不给条件即为默认查询所有,前端如果查询所有需要清理session缓存
     * 分页
     */
    @RequestMapping("/admin/getUsersByCon")
    public String getUsersByCon(Model model,@RequestParam(value = "con",defaultValue = "") String con,
                                @RequestParam(value = "page",defaultValue = "1") Integer page,
                                HttpSession session){
//        session.setAttribute("con",con);
        PageHelper.startPage(page,10);
        List<User> users = admin_userManageService.getUsersByCon(con);

//        System.out.println("users = " + users);

        PageInfo pageInfo = new PageInfo(users);
        model.addAttribute("pageinfo",pageInfo);
        model.addAttribute("con",con);
        return "/admin/admin_user";
    }

    /**
     * 用户删除
     *
     */
    @RequestMapping("/admin/deleteUser")
    public String deleteUser(Integer uno){
        admin_userManageService.deleteUser(uno);
        return "success";
    }

    /**
     * 用户编辑
     */
    @RequestMapping("/admin/updateUser")
    public String updateUser(User user){
        admin_userManageService.updateUser(user);
        return "redirect:/admin/getUsersByCon";
    }


    /**
     * 查询用户捐赠总资金
     * 不给条件即为默认查询所有,前端如果查询所有需要清理session缓存
     * 顺序排列
     * 分页
     */
    @RequestMapping("/admin/getUsersMoney")
    public String getUsersMoney(Model model,@RequestParam(value = "noc",defaultValue = "") String noc,
                                @RequestParam(value = "page",defaultValue = "1") Integer page
                                ){
        PageHelper.startPage(page,10);
        List<User> users = admin_userManageService.getUsersMoney(noc);


        PageInfo pageInfo = new PageInfo(users);
        model.addAttribute("pageinfo",pageInfo);
        model.addAttribute("noc",noc);
        return "/admin/admin_usermoney";
    }
    /**
     * 查询用户详细金额
     * 分页
     */
    @RequestMapping("/admin/getUserDonate")
    public String getUserDonate(Model model, String uno,
                                @RequestParam(value = "page",defaultValue = "1") Integer page){
        PageHelper.startPage(page,10);
        List<Donate> donates = admin_userManageService.getUserDonate(uno);

//        System.out.println("donates = " + donates);

        PageInfo pageInfo = new PageInfo(donates);
        model.addAttribute("pageinfo",pageInfo);
        return "success";
    }

    /**
     * 查询所有实名审核
     * 升序排序
     * 分页
     */
    @RequestMapping("/admin/getUsersReal")
    public String getUserReal(Model model,
                              @RequestParam(value = "page",defaultValue = "1") Integer page){
        PageHelper.startPage(page,10);
        List<Real> reals = admin_userManageService.getUserReal();

//        System.out.println("reals = " + reals);

        PageInfo pageInfo = new PageInfo(reals);
        model.addAttribute("pageinfo",pageInfo);
        return "/admin/admin_userReal";
    }

    /**
     * 不通过审核
     * rno找到对应实名信息行
     * uno找到对应user
     * 把数据库real的status从1变成2
     * user的realstatus变成2
     */
    @RequestMapping("/admin/nopassReal")
    public String nopassReal(Integer rno,Integer uno){
        admin_userManageService.updateNopassReal(rno,uno);
        return "redirect:/admin/getUsersReal";
    }

    /**
     * 通过实名审核
     * rno找到对应实名信息行
     * uno找到对应user
     * 把数据库real的status从1变成3
     * user的realstatus变成3
     */
    @RequestMapping("/admin/passReal")
    public String passReal(Integer rno,Integer uno){
        admin_userManageService.updatePassReal(rno,uno);
        return "redirect:/admin/getUsersReal";
    }
}
